package eban.storebyrules;

import eban.storebyrules.PersistenceException;
import eban.storebyrules.Storage;
import eban.storebyrules.Transaction;
import eban.storebyrules.filters.AbstractFilter;
import eban.storebyrules.filters.Filter;


import java.util.List;
import java.util.Map;

/**
 * This is the weaker contract you can define
 * in your objects to make them persistent.
 * The only requirement on the part of your objects is that
 * they need at least one field marked with Key annotation.
 * <p>
 * If you need other field to be persistent,
 * you have to mark them with Persistent annotation.
 * <p>
 * You must set your key field before you call ant of
 * the @see store or @see delete methods.
 * <p>
 * This contract does not maintain reference counting
 * information on database, thus instances of this class
 * cannot be garbage collected on storage. When you want to remove
 * an instance from the storage, you have to call @delete on that
 * instance.
 * <p>
 * Note to implementers:
 *
 *
 */
public interface PersistentCollection<TBean> {
    /**
     * Retrieve the storage driver used by this Contract instance
     * @return the Storage instance used by this Contract.
     * Granted to be non null.
     */
    Storage getStorage();

    /**
     * Retrieve the class of instances actually managed by this Contract
     * @return a Class representing the type of instances managed by this Contract
     * Granted to be non null.
     */
    Class<TBean> getClazz();

    /**
     * Retrieve all instances actually present in the storage.
     * @return a List containing all the instances actually present in the storage.
     * @throws eban.storebyrules.PersistenceException
     */
    List<TBean> load() throws PersistenceException;

    List<TBean> load(AbstractFilter filter) throws PersistenceException;

    void delete(Transaction transaction, TBean instance) throws PersistenceException;

    void clear(Transaction transaction);

    void store(Transaction transaction, TBean instance) throws PersistenceException;

   /* String keyName();

    String keyValue(TBean instance);*/

    Map<String, String> getFieldsMap(TBean instance) throws PersistenceException;

    String createTableSql();

    void detach(TBean instance);
}
